"""em

Revision ID: fc947874739f
Revises: cf27d1113f20
Create Date: 2023-08-19 14:47:09.743457

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import MetaData, Table

from src.modules.account.models.db.permission import Permission
from src.repository.migrations.utils import get_permission_id

# revision identifiers, used by Alembic.
revision = 'fc947874739f'
down_revision = 'cf27d1113f20'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('em_collection',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'),
                              nullable=False),
                    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
                    sa.Column('key', sa.String(length=64), nullable=False),
                    sa.Column('title', sa.String(length=64), nullable=False),
                    sa.Column('status', sa.String(length=64), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('key')
                    )
    op.create_table('em_source',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'),
                              nullable=False),
                    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
                    sa.Column('collection_id', sa.Integer(), nullable=False),
                    sa.Column('title', sa.String(length=64), nullable=False),
                    sa.Column('source_type', sa.String(length=64), nullable=False),
                    sa.Column('resolve_type', sa.String(length=64), nullable=False),
                    sa.Column('resolve_config', sa.String(length=4096), nullable=False),
                    sa.Column('summary', sa.String(length=4096), nullable=False),
                    sa.Column('content', sa.Text(), nullable=False),
                    sa.Column('status', sa.String(length=64), nullable=True),
                    sa.PrimaryKeyConstraint('id')
                    )

    permission_table = Table('sys_permission', Permission.metadata)

    admin_id = 1

    op.bulk_insert(permission_table, [
        {'title': '嵌入管理', 'code': 'embeddings', 'parent_id': admin_id},
    ])

    em_id = get_permission_id(op, "embeddings")
    print("em_id", em_id)
    op.bulk_insert(permission_table, [
        {'title': '知识库管理', 'code': 'embeddings:collection', 'parent_id': em_id},
        {'title': '源管理', 'code': 'embeddings:source', 'parent_id': em_id}
    ])

    parent_id = get_permission_id(op, "embeddings:collection")
    op.bulk_insert(permission_table, [
        {'title': '查看', 'code': 'embeddings:collection:view', 'parent_id': parent_id},
        {'title': '添加', 'code': 'embeddings:collection:add', 'parent_id': parent_id},
        {'title': '修改', 'code': 'embeddings:collection:update', 'parent_id': parent_id},
        {'title': '删除', 'code': 'embeddings:collection:delete', 'parent_id': parent_id},
    ])
    parent_id = get_permission_id(op, "embeddings:source")
    op.bulk_insert(permission_table, [
        {'title': '查看', 'code': 'embeddings:source:view', 'parent_id': parent_id},
        {'title': '添加', 'code': 'embeddings:source:add', 'parent_id': parent_id},
        {'title': '修改', 'code': 'embeddings:source:update', 'parent_id': parent_id},
        {'title': '删除', 'code': 'embeddings:source:delete', 'parent_id': parent_id},
    ])
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    op.drop_table('em_source')
    op.drop_table('em_collection')
    # ### end Alembic commands ###
